import { ref, onMounted, computed, watch } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
import { scrollStore } from '../stores/scrollTop'
import { cityStore, cityNumStore } from '@/stores/city'
import { cityNumber } from '@/api/follow'
import { getArticle } from '@/api/follow'
import { articleStore } from '@/stores/article'
import router from '@/router'



function datalist() {
    let number: any = ref()//地区编码
    onBeforeRouteLeave((to, from, next) => {
        if (to.name == 'detail') {
            // 如果进入的路由的名字为详情页面，记录当前窗口的滚动距离，切改变pinia的值
            scrollStore().$state.scroll = document.documentElement.scrollTop
            // console.log(scrollStore().$state.scroll);
        }
        next()
    })
    const city = computed(() => cityStore().$state.city)
    watch(city, (newValue, oldValue) => {
        cityNumberFun()
    });
    // let city: string = cityStore().$state.city//获取pinia的地区名字


    let nameList: Array<string> = ['曾祥科', '户云鑫睿', '赖炜锋', '小明', '亚瑟摩根', '哆啦A梦', '野比大雄', '马里奥', '八级大狂风', '路易吉', '库霸王', '断了的Xuan', '吉川裤浪']//用户名字列表
    let commentList = ['cool！', '我真的超爱这个公司的！', '假的假的！', '买家秀和卖家秀！', '设计师我想认识你！', '想问问这个设计师怎样', '你人还怪好捏', '棒棒哒~', '我比这家公司更便宜，建议联系我', '你好，能小刀一下吗', '我上我也行', '能不能便宜点', '来我房间，带你康点好康的']//评论列表
    let articleList: any = ref(null)//文章列表初始化
    const cityNumberFun: () => void = (): void => {//获取城市编码方法
        // console.log(111);

        cityNumber().then((data: any) => {//获取城市列表[{城市名}:'城市编码']
            for (var i in data) {
                if (Object.keys(data[i])[0] === city.value) {//如果value==city，拿到他的key值就是城市编码
                    number.value = Object.values(data[i])[0]
                }
            }
            const getArticleFun: () => void = (): void => {//拿到城市编码之后，发送网络请求
                // console.log(number.value);
                cityNumStore().$state.cityNum = number.value//改变pinia里面的城市编码,
                getArticle({ cityCode: number.value }).then((data: any) => {
                    articleList.value = data.result.data
                    if (localStorage.getItem("commentList") !== null) {
                        let myArticle: any = JSON.parse(window.localStorage.getItem("commentList") || '');
                        // console.log(myArticle);

                        myCommentList.value = myArticle.filter((item: any) => {
                            return item.cityNum == number.value
                        })
                        // console.log(myCommentList.value);
                    }
                    articleList.value.forEach((item: any) => {
                        let userCommentList: Array<object> = []//随机数生成的评论列表
                        let userCommentListLength = Math.ceil(Math.random() * 4)//评论数组长度0-4
                        for (let i = 0; i < userCommentListLength; i++) {
                            let commentIndex = Math.ceil(Math.random() * 12)  //评论下标
                            let userIndex = Math.ceil(Math.random() * 12)//用户下标
                            let user: any = {}
                            user[nameList[userIndex]] = commentList[commentIndex]//自定义对象
                            userCommentList.push(user)
                        }
                        item['comment'] = userCommentList
                    })
                    articleStore().$state.article = articleList.value//将当前文章列表存入store里面
                    // console.log(articleList.value);//文章列表
                })
            }
            getArticleFun()
        })

    }
    const goPage = (path: any, pid: any) => {
        // console.log(pid);
        router.push({
            path,
            query: {
                pid
            }
        })
    }
    let myCommentList: any = ref(null);

    // onMounted(() => {
    cityNumberFun()
    // })


    return {
        articleList,
        goPage,
        number,
        myCommentList,
    };
}


export default {
    datalist
}